<?php

namespace app\models;

use yii;
use app\components\BaseActiveRecord;

/**
 * This is the model class for table "{{%setting}}".
 *
 * @property string $id
 * @property string $val
 */
class Setting extends BaseActiveRecord {

    protected $cacheKey = '__settings';
    protected static $settings;

    public function init() {
        parent::init();
        $this->flush();
    }

    /**
     * @inheritdoc
     */
    public static function tableName() {
        return '{{%setting}}';
    }

    /**
     * @inheritdoc
     */
    public function rules() {
        return [
//            [['ali_pay_partner', 'ali_pay_key', 'wx_pay_mch_id', 'wx_pay_key', 'bank_name', 'bank_uname', 'bank_card', 'register_sms', 'register_pwd_sms', 'reset_pwd_sms', 'order_free_info', 'wx_appId', 'wx_appSecret', 'wx_token', 'qq_clientId', 'qq_clientSecret', 'weibo_clientId', 'weibo_clientSecret', 'wx_clientId', 'wx_clientSecret', 'service_qq'], 'required'],
            [['service_qq', 'service_tel'], 'required'],
            [['bank_name', 'bank_uname', 'bank_card'], 'required'],
            [['id'], 'string', 'max' => 120],
            [['val'], 'string', 'max' => 1024]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels() {
        return [
            'web_name' => '网站名称',
            'seo_title' => 'SEO标题',
            'seo_keyword' => 'SEO关键词',
            'seo_description' => 'SEO描述',
            'web_copyright' => '网站版权信息',
            'service_tel' => '客服电话',
            'service_qq' => '客服QQ',
            'order_free_info' => '法律文书服务介绍',
            'site_product_id' => 'PC首页服务项目',
            'register_sms' => '注册短信模板',
            'register_pwd_sms' => '注册发送密码模板',
            'reset_pwd_sms' => '找回密码短信模板',
            'ali_pay_partner' => '支付宝签约账号',
            'ali_pay_key' => '支付宝MD5密钥',
            'wx_pay_mch_id' => '微信支付商户号',
            'wx_pay_key' => '微信支付密钥',
            'bank_name' => '开户行(转账汇款)',
            'bank_uname' => '账户名',
            'bank_card' => '账号',
            'wx_appId' => '微信公众平台APPID',
            'wx_appSecret' => '微信公众平台APPSecret',
            'wx_token' => '微信公众平台token',
            'qq_clientId' => 'QQ平台clientId',
            'qq_clientSecret' => 'QQ平台clientSecret',
            'weibo_clientId' => '微博平台clientId',
            'weibo_clientSecret' => '微博平台clientSecret',
            'wx_clientId' => '微信平台clientId',
            'wx_clientSecret' => '微信平台clientSecret',
            'law_one_allow_sms' => '服务商初审通过短信',
            'law_one_deny_sms' => '服务商初审拒绝短信',
            'law_two_allow_sms' => '服务商资格审核通过短信',
            'law_two_deny_sms' => '服务商资格审核拒绝短信',
            'reg_coupon_money' => '注册送优惠券金额(￥0 不送优惠券)',
            'reg_coupon_expire' => '注册送优惠券有效期(天)',
            'agreement' => '服务协议',
            'coupon_info' => '优惠券使用规则',
        ];
    }

    public function __get($name) {
        return self::get($name);
    }

    public static function get($name, $default = NULL) {
        return isset(self::$settings[$name]) ? self::$settings[$name]['val'] : $default;
    }

    public function flush($force = FALSE) {
        self::$settings = Yii::$app->cache->get($this->cacheKey);
        if ($force || empty(self::$settings)) {
            $settings = static::find()->asArray()->indexBy('id')->all();
            Yii::$app->cache->set($this->cacheKey, $settings);
            self::$settings = $settings;
        }
    }
}
